package com.website.main.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.website.main.service.S_Role;
import com.website.mybatis.dao.PowerDao;
import com.website.mybatis.dao.RoleDao;
import com.website.mybatis.dao.RolePowerDao;
import com.website.mybatis.entity.SysRole;
import com.website.mybatis.entity.SysRolePower;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
public class S_RoleImpl implements S_Role {

    @Autowired
    RoleDao dao;
    @Autowired
    RolePowerDao rolePowerDao;
    @Autowired
    PowerDao powerDao;

    @Override
    public Map<String, Object> getList(String name, int page, int limit) {
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<SysRole> w = Wrappers.query(new SysRole(name));
        map.put("data", dao.selectPage(new Page<>((page - 1) * limit, limit), w));
        map.put("count", dao.selectCount(w));
        return map;
    }

    @Override
    public Map<String, Object> save(SysRole role) {
        Map<String, Object> map = new HashMap<>();
        if (dao.selectCount(Wrappers.query(new SysRole(role.getName()))) > 0) {
            map.put("success", false);
            map.put("msg", "角色名重复");
            return map;
        }
        int count;
        try {
            count = dao.insert(role);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success", false);
            map.put("msg", e.getMessage());
            return map;
        }
        if (count > 0) {
            map.put("success", true);
            map.put("msg", "新增角色成功");
            return map;
        }
        map.put("success", false);
        map.put("msg", "操作失败");
        return map;
    }

    @Override
    public Map<String, Object> delete(Integer id) {
        Map<String, Object> map = new HashMap<>();
        SysRole role = dao.selectById(id);
        if (role == null) {
            map.put("success", false);
            map.put("msg", "相应角色不存在");
            return map;
        }
        if (dao.deleteById(id) > 0) {
            map.put("success", true);
            map.put("msg", "操作成功");
            return map;
        }
        map.put("success", false);
        map.put("msg", "操作失败");
        return map;
    }

    @Override
    public Map<String, Object> update(SysRole r) {
        Map<String, Object> map = new HashMap<>();
        SysRole role = dao.selectById(r.getId());
        if (role == null) {
            map.put("success", false);
            map.put("msg", "相应角色不存在");
            return map;
        }
        BeanUtils.copyProperties(r, role);
        if (dao.updateById(role) > 0) {
            map.put("success", true);
            map.put("msg", "更新成功");
        } else {
            map.put("success", false);
            map.put("msg", "更新失败");
        }
        return map;
    }

    @Override
    public Map<String, Object> getAllRoles() {
        Map<String, Object> map = new HashMap<>();
        map.put("data", dao.selectList(Wrappers.<SysRole>emptyWrapper()));
        return map;
    }

    @Transactional
    @Override
    public Map<String, Object> updPowers(Integer id, Integer[] powers) {
        Map<String, Object> map = new HashMap<>();
        SysRole role = dao.selectById(id);
        if (role == null) {
            map.put("success", false);
            map.put("msg", "相应角色不存在");
            return map;
        }
        rolePowerDao.delete(Wrappers.<SysRolePower>query().eq("role_id", id));
        for (Integer i : powers) {
            rolePowerDao.insert(new SysRolePower(id, i));
        }
        if (dao.updateById(role) > 0) {
            map.put("success", true);
            map.put("msg", "更新成功");
        } else {
            map.put("success", false);
            map.put("msg", "更新失败");
        }
        return map;
    }

    @Override
    public SysRole getRoleById(Integer roleId) {
        return dao.selectRoleAndPowers(roleId);
    }

}
